package com.shxc.smarttrade.index.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.index.model.NoticeModel;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 首页 - 消息中心 - 公告列表
 */

public class NoticeAdapter extends RecyclerView.Adapter<NoticeAdapter.ViewHolder> {

    private Context mContext;
    private List<NoticeModel.DataBean> mList;
    private Handler mHandler;

    public NoticeAdapter(Context c, List<NoticeModel.DataBean> list, Handler handler) {
        mContext = c;
        mList = list;
        mHandler = handler;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_notice, parent, false);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        final NoticeModel.DataBean model = mList.get(position);
        if (position==0) {
            holder.first_view.setVisibility(View.VISIBLE);
        }else{
            holder.first_view.setVisibility(View.GONE);
        }
        holder.title_textview.setText(model.getTitle());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        holder.time_textview.setText(sdf.format(new Date(model.getCreatedAt())));

        //跳转页面
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = new Message();
                msg.what = 1;
                msg.obj = model;
                mHandler.sendMessage(msg);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public void upDate(List<NoticeModel.DataBean> list) {
        mList.clear();
        for (int i = 0; i < list.size(); i++) {
            mList.add(list.get(i));
        }
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        protected View first_view;
        protected TextView title_textview;
        protected TextView time_textview;

        public ViewHolder(View itemView) {
            super(itemView);
            first_view = itemView.findViewById(R.id.first_view);
            title_textview = itemView.findViewById(R.id.title_textview);
            time_textview = itemView.findViewById(R.id.time_textview);
        }
    }

}
